#include <portability/portimpl.h>

#include <x3manager/portability/x3port.h>
//#include <module/pluginimpl.h>

#include <x3manager/nonplugin/useplugins.h>

#include <x3manager/objptr.h>
#include <FreeEarth/IFreeEarth.h>

int test();

HMODULE modules[10] = { NULL };

int main()
{
	//const char* plugins[] = { 
	//	"x3manager.pln", 
	//	"FreeEarth.pln", 
	//	"FreeMeasure.pln",
	//	NULL
	//};
	//int count = 0;

	//for (int i = 0; plugins[i]; i++)
	//{
		//const char* filename = plugins[i];
		//if (PathIsRelativeA(filename))
		//{
		//	GetModuleFileNameA(basemod, fullname, MAX_PATH);
		//	PathRemoveFileSpecA(fullname);
		//	PathAppendA(fullname, folder);
		//	PathAppendA(fullname, filename);
		//	//_hmod = x3LoadLibrary(fullname);
		//}
		//modules[count] = x3LoadLibrary(plugins[i]);
		//if (modules[count])
		//	count++;
	//}

	//x3::loadPlugins(plugins, "");

	//int ret = test();

	//while (--count >= 0)
	//{
	//	x3FreeLibrary(modules[count]);
	//}

	//x3::unloadPlugins();

	//return ret;
	const char* plugins[] = {
		"x3manager.pln", "FreeEarth.pln", "FreeAnalysis.pln", NULL
	};

	x3::AutoLoadPlugins autoload(plugins, "");

	x3::Object<IFreeEarth> p(clsidFreeEarth);

	if (p.valid())
	{
		p->Initialize();
	}

	if (p->GetRenderContext())
	{
		while (1)
		{
			p->GetRenderContext()->Frame();
		}
	}

	/*while (1)
	{
	p->Frame();
	}
	*/

	/*const char* otherPlugins[] = {
		"FreeMeasured.pln", NULL
	};

	x3::AutoLoadPlugins autoloadOthers(otherPlugins, "");
*/

	//test();
	return 1;
}

#include <stdio.h>

int test()
{
	x3::Object<IFreeEarth> p(clsidFreeEarth);

	if (p)
	{
		printf("The plugin is loaded (%s in %s).\n", 
			p->getInterfaceName(), p->getClassName());
	}
	else
	{
		printf("The plugin is not loaded.\n");
		return 1;
	}

	//int sum = p->Initialize();
	//printf("p->Initialize(): %d\n", sum);

	/*while (1)
	{
	p->Frame();
	}*/

	printf("Goodbye\n");
	return 0;
}
